<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Post_mgr extends MY_Controller {
	public function page_index($post_id) {
		$this->load->model('post_model');
		$post = $this->post_model->get_by_id($post_id);
		if (empty($post)) {
			$this->page_data['nav_index'] = 1;
			$this->page_data['bread_crumb'] = array(
				array('label' => '首页')
			);
		} else {
			//$post['normal_rate'] = calc_rate($post['c1_count'], $post['c2_count']);
			$this->load->model('category_model');
			$category = $this->category_model->get_by_id($post['cate_id']);
			$this->page_data['category'] = $category;
			
			if ($post['is_anonymous'] == 0) {
				$this->load->model('user_model');
				$author = $this->user_model->get_by_id($post['user_id']);
				$this->page_data['author'] = $author;
			}
			
			$login_user_id = (int) $this->session->userdata(KEY_USER_ID);
			$is_voted = $this->post_model->is_voted($post_id, $login_user_id);
			$this->page_data['is_voted'] = $is_voted;
			
			$this->load->model('comment_model');
			$comment_list = $this->comment_model->get_all_list_by_post($post_id);
			$comment_tree_html = $this->_build_comment_tree_html($comment_list);
			$this->page_data['comment_tree_html'] = $comment_tree_html;
			
			$post_title = htmlspecialchars($post['title']);
			$this->page_data['nav_index'] = 1;
			$this->page_data['bread_crumb'] = array(
				array('label' => '首页', 'href' => site_url()),
				array('label' => "{$category['name']}", 'href' => site_url("category/{$category['id']}_1.html")),
				array('label' => "{$post_title}，这正常吗？"),
			);
		}
		$this->page_data['post'] = $post;
		
		$this->page_data['meta_content'] = $this->load->view('post/meta', $this->page_data, TRUE);
		$this->page_data['main_content'] = $this->load->view('post/main', $this->page_data, TRUE);
		$this->load->view('template/main', $this->page_data);
	}
	
	public function page_submit() {
		$page_title = '发布';
		$this->page_data['nav_index'] = 2;
		$this->page_data['page_title'] = $page_title;
		$this->page_data['bread_crumb'] = array(
			array('label' => '首页', 'href' => site_url()),
			array('label' => $page_title)
		);
		$this->page_data['meta_content'] = $this->load->view('post/submit/meta', $this->page_data, TRUE);
		$this->page_data['main_content'] = $this->load->view('post/submit/main', $this->page_data, TRUE);
		$this->load->view('template/main', $this->page_data);
	}
	
	public function ajax_submit() {
		$this->_check_ajax_login();
		$user_id = (int) $this->session->userdata(KEY_USER_ID);
		$cate_id = (int) $this->input->post('cate_id');
		if ($cate_id <= 0) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '请选择分类！');
			echo json_encode($ret); exit;
		}
		$title = $this->input->post('title');
		$title = trim($title);
		if (empty($title)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '请输入标题！');
			echo json_encode($ret); exit;
		}
		$anonymous = (int) $this->input->post('anonymous');
		$detail = $this->input->post('detail');
		$detail = trim($detail);
		if (empty($detail)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '请输入详细内容！');
			echo json_encode($ret); exit;
		}
		
		$this->load->model('post_model');
		$ret = array('status' => STATUS_SUCCESS, 'msg' => '');
		$insert_id = $this->post_model->add(array(
			'user_id' => $user_id,
			'cate_id' => $cate_id,
			'title' => $title,
			'is_anonymous' => $anonymous,
			'type' => 1,
			'detail' => $detail,
			'status' => DB_STATUS_NORMAL,
			'create_time' => date('Y-m-d H:i:s')
		));
		if ($insert_id > 0) {
			$ret['msg'] = '发布成功！';
			$ret['data'] = site_url("post/{$insert_id}.html");
		} else {
			$ret = array('status' => STATUS_ERROR, 'msg' => '发布失败请重试！');
		}
		
		echo json_encode($ret);
	}
	
	public function ajax_add_comment() {
		$this->_check_ajax_login();
		$login_user_id = (int) $this->session->userdata(KEY_USER_ID);
		$post_id = (int) $this->input->post('post_id');
		$this->_validate_post_id($post_id);
		$reply_id = (int) $this->input->post('reply_id');
		$this->_validate_reply_comment_id($reply_id);
		$content = $this->input->post('content');
		$content = trim($content);
		$this->_validate_content($content);
		
		$this->load->model('comment_model');
		$ret = array('status' => STATUS_SUCCESS, 'msg' => '');
		$insert_id = $this->comment_model->add(array(
			'post_id' => $post_id,
			'reply_id' => $reply_id,
			'user_id' => $login_user_id,
			'content' => $content,
			'status' => DB_STATUS_NORMAL,
			'create_time' => date('Y-m-d H:i:s')
		));
		if ($insert_id > 0) {
			$comment = $this->comment_model->get_by_id($insert_id);
			$ret['data'] = $this->load->view('include/comment_media', array('login_user_id' => $login_user_id, 'comment' => $comment, 'nested_media' => ''), TRUE);
		} else {
			$ret = array('status' => STATUS_ERROR, 'msg' => '评论失败，请重试！');
		}
		
		echo json_encode($ret);
	}
	
	public function ajax_vote_up_comment() {
		$this->_check_ajax_login();
		$login_user_id = (int) $this->session->userdata(KEY_USER_ID);
		$comment_id = (int) $this->input->post('comment_id');
		$this->load->model('comment_model');
		if ($this->comment_model->belongs_to_user($comment_id, $login_user_id)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '不能赞自己的评论哦 :P');
			echo json_encode($ret); exit;
		}
		if ($this->comment_model->is_gooded($comment_id, $login_user_id)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '已经赞过就不能再赞了哦 :P');
			echo json_encode($ret); exit;
		}
		$ret = array('status' => STATUS_SUCCESS, 'msg' => '');
		$this->comment_model->inc_good_count($comment_id, $login_user_id);
		
		echo json_encode($ret);
	}
	
	public function ajax_delete_comment() {
		$this->_check_ajax_login();
		$login_user_id = (int) $this->session->userdata(KEY_USER_ID);
		$comment_id = (int) $this->input->post('comment_id');
		$this->load->model('comment_model');
		if (!$this->comment_model->belongs_to_user($comment_id, $login_user_id)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '评论无法被删除！');
			echo json_encode($ret); exit;
		}
		
		$ret = array('status' => STATUS_SUCCESS, 'msg' => '');
		$this->comment_model->delete($comment_id);
		
		echo json_encode($ret);
	}
	
	public function ajax_vote() {
		$this->_check_ajax_login();
		$login_user_id = (int) $this->session->userdata(KEY_USER_ID);
		$post_id = (int) $this->input->post('post_id');
		$this->_validate_post_id($post_id);
		$choice_num = $this->input->post('choice_num');
		$this->_validate_choice($choice_num);
		$this->load->model('post_model');
		if ($this->post_model->belongs_to_user($post_id, $login_user_id)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '不能给自己投票哦 :P');
			echo json_encode($ret); exit;
		}
		if ($this->post_model->is_voted($post_id, $login_user_id)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '已经投过票，就不能再投了哦 :P');
			echo json_encode($ret); exit;
		}
		
		$ret = array('status' => STATUS_SUCCESS, 'msg' => '');
		$this->post_model->vote($post_id, $login_user_id, $choice_num);
		
		echo json_encode($ret);
	}
	
	public function ajax_show_vote_result() {
		$this->_check_ajax_login();
		$post_id = (int) $this->input->post('post_id');
		$this->_validate_post_id($post_id);
		$login_user_id = (int) $this->session->userdata(KEY_USER_ID);
		$this->load->model('post_model');
		if (!$this->post_model->is_voted($post_id, $login_user_id)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '您没有投过票，还不能看结果哦 :P');
			echo json_encode($ret); exit;
		}
		$post = $this->post_model->get_by_id($post_id);
		$ret = array('status' => STATUS_SUCCESS, 'msg' => '');
		$ret['data'] = $this->load->view('include/vote_bar', array('post' => $post), TRUE);
		
		echo json_encode($ret);
	}
	
	private function _build_comment_tree($comment_list, $reply_id = 0) {
		$comment_tree = array();
		foreach ($comment_list as $comment) {
			if ($comment['reply_id'] == $reply_id) {
				$comment_tree[] = array(
					'user_id' => $comment['user_id'],
					'content' => $comment['content'],
					'create_time' => $comment['create_time'],
					'children' => $this->_build_comment_tree($comment_list, $comment['comment_id'])
				);
			}
		}
		
		return $comment_tree;
	}
	
	private function _build_comment_tree_html($comment_list, $reply_id = 0) {
		$html = '';
		foreach ($comment_list as $comment) {
			if ($comment['reply_id'] == $reply_id) {
				$data = array(
					'login_user_id' => $this->page_data['login_user_id'],
					'comment' => $comment,
					'nested_media' => $this->_build_comment_tree_html($comment_list, $comment['comment_id'])
				);
				$html .= $this->load->view('include/comment_media', $data, TRUE);
			}
		}
		
		return $html;
	}
	
	private function _validate_post_id($post_id) {
		if ($post_id <= 0) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '非法的post id！');
			echo json_encode($ret); exit;
		}
		
		$this->load->model('post_model');
		if (!$this->post_model->exists_id($post_id)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '文章不存在，或者已被删除！');
			echo json_encode($ret); exit;
		}
	}
	
	private function _validate_reply_comment_id($comment_id) {
		if ($comment_id < 0) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '非法的评论ID！');
			echo json_encode($ret); exit;
		}
		if ($comment_id > 0) {
			$this->load->model('comment_model');
			if (!$this->comment_model->exists_id($comment_id)) {
				$ret = array('status' => STATUS_ERROR, 'msg' => '评论不存在，或者已被删除！');
				echo json_encode($ret); exit;
			}
		}
	}
	
	private function _validate_content($content) {
		if (strlen($content) <= 0) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '评论不能为空！');
			echo json_encode($ret); exit;
		}
	}
	
	private function _validate_choice($choice_num) {
		$choice_enum = array(1, 2);
		if (!in_array($choice_num, $choice_enum)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '非法的选项！');
			echo json_encode($ret); exit;
		}
	}
}